# -*- coding: UTF-8 -*-
from collections import defaultdict
from typing import List
from leetcode_helper import TestcaseHelper, SolutionBase, Stack


class Solution(SolutionBase):
    # 解题的方法，命名和Leetcode的保持一致即可
    def restoreArray(self, adjacentPairs: List[List[int]]) -> List[int]:
        cnt_dict = defaultdict(int)
        item_dict = defaultdict(list)

        for a, b in adjacentPairs:
            cnt_dict[a] += 1
            cnt_dict[b] += 1
            item_dict[a].append(b)
            item_dict[b].append(a)

        start = 0
        for key, value in cnt_dict.items():
            if value == 1:
                start = key
                break

        nums_len = len(adjacentPairs) + 1
        ans = [0] * nums_len
        ans[0] = start
        ans[1] = item_dict[start][0]

        for i in range(2, nums_len):
            values = item_dict[ans[i-1]]
            for value in values:
                if value != ans[i-2]:
                    ans[i] = value

        return ans

    def check_result(self, expect, result) -> bool:
        return self.check_result_array(expect, result)


if __name__ == '__main__':
    solution = Solution()

    # 按照leetcode的题目说明，将基本的test case填入
    # 每条case的最后一个元素是正确的结果，即期待的结果
    # 使用元组来保存输入的数据，期待的结果
    testcases = [
        ([[69922,86539],[-15590,-22801],[72138,-28458],[-15590,-33097],[-28458,19197],[30152,69922],[-33097,-56099],[84189,-22801],[-56099,-58156],[72138,38249],[30152,-81777],[38249,84189],[-55922,19197],[-55922,86539]], [-81777,30152,69922,86539,-55922,19197,-28458,72138,38249,84189,-22801,-15590,-33097,-56099,-58156]),
        ([[-3,-9],[-5,3],[2,-9],[6,-3],[6,1],[5,3],[8,5],[-5,1],[7,2]], [7,2,-9,-3,6,1,-5,3,5,8]),
        ([[4, -2], [1, 4], [-3, 1]], [-2, 4, 1, -3]),
        ([[2, 1], [3, 4], [3, 2]], [1, 2, 3, 4]),
        ([[100000, -100000]], [100000, -100000])
    ]

    for case_item in testcases:
        # 获取测试用的case和期待的执行结果
        input1 = case_item[0]
        expect = case_item[len(case_item) - 1]
        exec_result = solution.restoreArray(input1)

        # 判断执行结果，输出提示信息
        check_result = solution.check_result(expect, exec_result)
        TestcaseHelper.print_case(check_result, case_item, exec_result)

